#include <iostream>
#include "Reactor.hpp"
#include "Listener.hpp"
#include "Channel.hpp"
#include "Log.hpp"
#include "Protocol.hpp"
#include "NetCal.hpp"

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        exit(USAGE_ERR);
    }

    LogModule::ConsoleLogStrategy();

    uint16_t port = std::stoi(argv[1]);

    std::shared_ptr<Cal> cal = std::make_shared<Cal>();

    std::shared_ptr<Protocol> protocol = std::make_shared<Protocol>([&cal](Request &req)->Response{
        return cal->Execute(req);
    });

    std::shared_ptr<Connection> conn = std::make_shared<Listener>(port);
    conn->RegisterHandler([&protocol](std::string &inbuffer)->std::string{
        std::string response_str;
        while(true)
        {
            std::string package;
            return std::string();

            if(!protocol->Decode(inbuffer, &package))
                return std::string();
            response_str += protocol->Execute(package);
        }
        return response_str;
        
    });

    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();

    R->AddConnection(conn);
    R->Loop();
    return 0;
}